package com.wang.controller;

import com.wang.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.wang.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("index");
        return mv;
    }
    @RequestMapping("/login")
    public ModelAndView login(String uname, String upassword, String role, HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        User user = userService.login(uname,upassword);


        if(user==null||!user.getRole().equals(role)){
            mv.setViewName("fail");
        }
        if(user!=null) {
            HttpSession session = request.getSession();
            session.setAttribute("uname", uname);
            session.setAttribute("upassword", upassword);
            session.setAttribute("uid",user.getUid());
            System.out.println(session.getAttribute("uid"));


            if (role.equals("parent")) {
                Parents parent=userService.findParent(user.getUid());
                String idcard=parent.getIdcard();
                if (idcard==null){
                mv.setViewName("addParentMessage");
                }else {
                    session.setAttribute("Parents",parent);
                    mv.setViewName("parentlogin");
                }
            }

            if (role.equals("teacher")) {
                Teacher teacher =userService.findTeacher(user.getUid());
                String idcard=teacher.getIdeard();
                if (idcard==null){
                    mv.setViewName("addTeacherMessage");
                }else {
                    session.setAttribute("Teacher",teacher);
                    mv.setViewName("teacherlogin");
                }
            }
        }
        System.out.println(mv.getViewName());
        return mv;
    }
    @RequestMapping("/AddUser")
    public ModelAndView adduser(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("AddUser");
        return mv;
    }
    @RequestMapping("/parentlogin")
    public ModelAndView parentlogin(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("parentlogin");
        return mv;
    }
    @RequestMapping("/teacherlogin")
    public ModelAndView teacherlogin(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("teacherlogin");
        return mv;
    }
    @RequestMapping("/zhuce")
    public ModelAndView zhuce(User user){
            ModelAndView mv = new ModelAndView();
            userService.addUser(user);
            if (user.getRole().equals("parent")) {
                User user1 = userService.selectUserbyName(user.getUname());
                int uid = user1.getUid();
                String uname = user1.getUname();
                userService.addParent(uid, uname);
            }
            if (user.getRole().equals("teacher")) {
                User user1 = userService.selectUserbyName(user.getUname());
                int uid = user1.getUid();
                String uname = user1.getUname();
                userService.addTeacher(uid, uname);
            }
            mv.setViewName("success");

        return mv;
    }



    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("uname");
        session.removeAttribute("upassword");
        return "index";

    }
    @RequestMapping("/addpm")
    public ModelAndView addpm(Parents parents ,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        String uid= String.valueOf(session.getAttribute("uid"));
        System.out.println(uid);
        int uid1=Integer.parseInt(uid);
        parents.setUid(uid1);
        userService.addParentMessage(parents);
        Parents parents1=userService.findParent(uid1);
        session.setAttribute("Parents",parents1);
        mv.setViewName("parentlogin");

        return mv;

    }
    @RequestMapping("/addtm")
    public ModelAndView addtm(Teacher teacher ,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        String uid= String.valueOf(session.getAttribute("uid"));
        System.out.println(uid);
        int uid1=Integer.parseInt(uid);
        teacher.setUid(uid1);
        userService.addTeacherMessage(teacher);
        Teacher teacher1=userService.findTeacher(uid1);
        session.setAttribute("Teacher",teacher1);
        mv.setViewName("teacherlogin");
        return mv;

    }

    @RequestMapping("/updateParentMessage")
    public ModelAndView updateParentMessage(HttpServletRequest request ) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Parents parents= (Parents) session.getAttribute("Parents");
        mv.addObject("Parents",parents);
        mv.setViewName("updateParentMessage");
        return mv;
    }
    @RequestMapping("/updateTeacherMessage")
    public ModelAndView updateTeacherMessage(HttpServletRequest request ) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher= (Teacher) session.getAttribute("Teacher");
        mv.addObject("Teacher",teacher);
        mv.setViewName("updateTeacherMessage");
        return mv;
    }

    @RequestMapping("/updatepm")
    public ModelAndView updatepm(Parents parents,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Parents parents1= (Parents) session.getAttribute("Parents");
        int uid=parents1.getUid();
        parents.setUid(uid);
        userService.addParentMessage(parents);
        mv.setViewName("success1");
        return mv;
    }
    @RequestMapping("/updatetm")
    public ModelAndView updatetm(Teacher teacher,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher1= (Teacher) session.getAttribute("Teacher");
        int uid=teacher1.getUid();
        teacher.setUid(uid);
        userService.addTeacherMessage(teacher);
        mv.setViewName("success2");
        return mv;
    }


    @RequestMapping("/studentMessage")
    public ModelAndView studentMessage(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Parents parents = (Parents) session.getAttribute("Parents");
        int pid = parents.getPid();
        List<Students> list = userService.findStudents(pid);
        mv.addObject("list", list);
        mv.setViewName("studentMessage");
        return mv;
    }
    @RequestMapping("/ltime")
    public ModelAndView ltime(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("Teacher");
        int tid = teacher.getTid();
        List<Lessionstime> list = userService.findLessiontime(tid);
        if(null == list || list.size() ==0 ){
            mv.setViewName("addltime");
        }else {
            mv.addObject("list", list);
            mv.setViewName("ltime");
        }
        return mv;
    }
    @RequestMapping("/lgrade")
    public ModelAndView lgrade(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("Teacher");
        int tid = teacher.getTid();
        List<Grade> list = userService.findGrade(tid);
        if(null == list || list.size() ==0 ){
            mv.setViewName("addlgrade");
        }else {
            mv.addObject("list", list);
            mv.setViewName("lgrade");
        }
        return mv;
    }
    @RequestMapping("/lproject")
    public ModelAndView lproject(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Teacher teacher = (Teacher) session.getAttribute("Teacher");
        int tid = teacher.getTid();
        List<Project> list= userService.findProject(tid);
        if(null == list || list.size() ==0 ){
            mv.setViewName("addlproject");
        }else {
            mv.addObject("list", list);
            mv.setViewName("lproject");
        }
        return mv;
    }


    @RequestMapping("/deleteStudent")
    public ModelAndView deleteStudent( Integer sid) {
        ModelAndView mv = new ModelAndView();
        userService.deleteStudent(sid);
        mv.setViewName("success1");
        return mv;
    }
    @RequestMapping("/deleteTime")
    public ModelAndView deleteTime( Integer lid) {
        ModelAndView mv = new ModelAndView();
        userService.deletetime(lid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/deleteGrade")
    public ModelAndView deleteGrade( Integer gid) {
        ModelAndView mv = new ModelAndView();
        userService.deletegrade(gid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/deleteProject")
    public ModelAndView deleteProject( Integer pid) {
        ModelAndView mv = new ModelAndView();
        userService.deleteproject(pid);
        mv.setViewName("success2");
        return mv;
    }



    @RequestMapping("/updateStudent")
    public ModelAndView updateStudent(Integer sid,HttpServletRequest request) {
        HttpSession session=request.getSession();
        System.out.println("sid");
        System.out.println(sid);
        ModelAndView mv = new ModelAndView();
        Students students= userService.findStudentsbysid(sid);
        session.setAttribute("Students", students);
        mv.addObject("Students", students);
        mv.setViewName("updateStudent");
        return mv;
    }
    @RequestMapping("/updateTime")
    public ModelAndView updateTime(Integer lid,HttpServletRequest request) {
        HttpSession session=request.getSession();
        ModelAndView mv = new ModelAndView();
        Lessionstime lessionstime=userService.findLessiontimeByid(lid);
        session.setAttribute("Lessionstime", lessionstime);
        mv.addObject("Lessionstime", lessionstime);
        mv.setViewName("updateTime");
        return mv;
    }
    @RequestMapping("/updateGrade")
    public ModelAndView updateGrade(Integer gid,HttpServletRequest request) {
        HttpSession session=request.getSession();
        ModelAndView mv = new ModelAndView();
        Grade grade=userService.findGradeByid(gid);
        session.setAttribute("Grade", grade);
        mv.addObject("Grade", grade);
        mv.setViewName("updateGrade");
        return mv;
    }
    @RequestMapping("/updateProject")
    public ModelAndView updateProject(Integer pid,HttpServletRequest request) {
        HttpSession session=request.getSession();
        ModelAndView mv = new ModelAndView();
        Project project=userService.findProjectByid(pid);
        session.setAttribute("Project", project);
        mv.addObject("Project", project);
        mv.setViewName("updateProject");
        return mv;
    }


    @RequestMapping("/updatesm")
    public ModelAndView updatesm(Students students,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
         Students students1= (Students) session.getAttribute("Students");
        int sid=students1.getSid();
        students.setSid(sid);
        userService.addStudent(students);
        mv.setViewName("success1");
        return mv;
    }
    @RequestMapping("/updatetimamessage")
    public ModelAndView updatetimamessage(Lessionstime lessionstime,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Lessionstime lessionstime1= (Lessionstime) session.getAttribute("Lessionstime");
        int lid=lessionstime1.getLid();
        lessionstime.setLid(lid);
        userService.updatetime(lessionstime);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/updategrademessage")
    public ModelAndView updategrademessage(Grade grade,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Grade grade1= (Grade) session.getAttribute("Grade");
        int gid=grade1.getGid();
        grade.setGid(gid);
        userService.updategrade(grade);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/updateprojectmessage")
    public ModelAndView updateprojectmessage(Project project,HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        HttpSession session=request.getSession();
        Project project1= (Project) session.getAttribute("Project");
        int pid=project1.getPid();
        project.setPid(pid);
        userService.updateproject(project);
        mv.setViewName("success2");
        return mv;
    }


    @RequestMapping("/addsm")
    public ModelAndView addsm(Students students ,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Parents parents= (Parents) session.getAttribute("Parents");
        int pid=parents.getPid();
        students.setPid(pid);
        userService.addStudentall(students);
        mv.setViewName("success1");
        return mv;
    }
    @RequestMapping("/addlt")
    public ModelAndView addlt(Lessionstime lessionstime ,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher= (Teacher) session.getAttribute("Teacher");
        int tid=teacher.getTid();
        lessionstime.setTid(tid);
        userService.addtime(lessionstime);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/addlg")
    public ModelAndView addlg(Grade grade,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher= (Teacher) session.getAttribute("Teacher");
        int tid=teacher.getTid();
        grade.setTid(tid);
        userService.addgrade(grade);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/addlp")
    public ModelAndView addlp(Project project,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher= (Teacher) session.getAttribute("Teacher");
        int tid=teacher.getTid();
        project.setTid(tid);
        userService.addproject(project);
        mv.setViewName("success2");
        return mv;
    }



    @RequestMapping("/addStudentMessage")
    public ModelAndView addStudentMessage(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("addStudentMessage");
        return mv;
    }

    @RequestMapping("/teacherMessage")
    public ModelAndView teacherMessage() {
        ModelAndView mv = new ModelAndView();
        List<Teacher> list = userService.findTeacherall();
        mv.addObject("list", list);
        mv.setViewName("teacherMessage");
        return mv;
    }
    @RequestMapping("/addOrder")
    public ModelAndView addOrder(Integer tid,HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        session.setAttribute("tid",tid);
        mv.setViewName("addOrder");
        return mv;
    }
    @RequestMapping("/findteacherorder")
    public ModelAndView findteacherorder(HttpServletRequest request,Integer tid) {
        ModelAndView mv = new ModelAndView();
        HttpSession session = request.getSession();
        Lessionstime lessionstime=userService.findLessiontimeOne(tid);
        Grade grade=userService.findGradeOne(tid);
        Project project=userService.findProjectOne(tid);
        if(lessionstime!=null&&grade!=null&&project!=null) {

        }
            String timestart = lessionstime.getTimestart();
            String timeend = lessionstime.getTimeend();
            String timeweek = lessionstime.getTimeweek();
            String tgrade = grade.getTgrade();
            String pname = project.getPname();
            session.setAttribute("timestart", timestart);
            session.setAttribute("timeend", timeend);
            session.setAttribute("timeweek", timeweek);
            session.setAttribute("tgrade", tgrade);
            session.setAttribute("pname", pname);

        mv.setViewName("findteacherorder");
        return mv;
    }
    @RequestMapping("/addom")
    public ModelAndView addom(String ograde,String oproject,Double price,String sname,HttpServletRequest request) throws ParseException {
        ModelAndView mv=new ModelAndView();
        Order order=new Order();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        String date = ft.format(new Date());
        Date date1 = ft.parse(date);
        System.out.println(date);
        HttpSession session = request.getSession();
        Parents parents= (Parents) session.getAttribute("Parents");
        Object tid=session.getAttribute("tid");
        int tid1= Integer.parseInt(tid.toString());
        int pid=parents.getPid();
        int sid=userService.findStudentsbyname(pid,sname);
        order.setOtime(date1);
        order.setOgrade(ograde);
        order.setPrice(price);
        order.setOproject(oproject);
        order.setSid(sid);
        order.setState(1);
        order.setTid(tid1);
        order.setPid(pid);
        userService.addorder(order);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/commitOrder")
    public ModelAndView commitOrder(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Teacher teacher= (Teacher) session.getAttribute("Teacher");
        int tid=teacher.getTid();
        List<Order> list = userService.findOrderbytid(tid);
        mv.addObject("list", list);
        mv.setViewName("commitOrder");
        return mv;
    }
    @RequestMapping("/teachercommit")
    public ModelAndView teachercommit(Integer oid){
        ModelAndView mv=new ModelAndView();
        userService.updatetorder(oid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/teachernocommit")
    public ModelAndView teachernocommit(Integer oid){
        ModelAndView mv=new ModelAndView();
        userService.updatetnoorder(oid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/commit")
    public ModelAndView commit(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        HttpSession session=request.getSession();
        Parents parents= (Parents) session.getAttribute("Parents");
        int pid=parents.getPid();
        List<Order> list=userService.findOrderbypid(pid);
        mv.addObject("list",list);
        mv.setViewName("commit");
        return mv;
    }
    @RequestMapping("/parentcommit")
    public ModelAndView parentcommit(Integer oid){
        ModelAndView mv=new ModelAndView();
        userService.updateporder(oid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/parentnocommit")
    public ModelAndView parentnocommit(Integer oid){
        ModelAndView mv=new ModelAndView();
        userService.updatepnoorder(oid);
        mv.setViewName("success2");
        return mv;
    }
    @RequestMapping("/ordermessage")
    public ModelAndView ordermessage(Integer sid){
        ModelAndView mv=new ModelAndView();
        Students students=userService.findStudentsbysid(sid);
        mv.addObject("students1",students);
        mv.setViewName("ordermessage");
        return mv;
    }
    @RequestMapping("/success1")
    public ModelAndView success1(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("success1");
        return mv;
    }
    @RequestMapping("/success2")
    public ModelAndView success2(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("success2");
        return mv;
    }

}
